package cll;

import org.apache.commons.math3.random.RandomDataImpl;

import static cll.Parameters.*;
import static java.lang.Math.random;

public class Centroblast extends Cell {
	
	private boolean GC = false;// a rencontré un T, fabrique un GC
	private boolean triggered = false; // a rencontré une FDC

	public Centroblast (Environment envt){
		type = Type.Centroblast;
		environment = envt;
		positionspeed = new PositionSpeed(random()*tailleGC, random()*tailleGC,0);
		antigen = Antigen.getRandomAntigen();
		rayon = rayonCB;
		//CD38 = taux_cd38;
	}
	
	
	public Centroblast(Centrocyt centrocyt) {
		type = Type.Centroblast;
		environment = centrocyt.environment;
		positionspeed = centrocyt.positionspeed;
		antigen = centrocyt.antigen;
		//CD38 = centrocyt.CD38;
		rayon = rayonCB;
	}



	public Centroblast(Centroblast centroblast) {
		type = Type.Centroblast;
		environment = centroblast.environment;
		positionspeed = centroblast.positionspeed.copy();
		antigen = centroblast.antigen.copy();
		//CD38 = centroblast.CD38;
		rayon = rayonCB;
	}



	public void changeDirection() {
		if(Math.random() < dt/duree_persistence_deplacement_CB){
			Place myplace = environment.getPlace(positionspeed);
			//System.out.println(myplace.getGradx());
			//System.out.println(myplace.getChemokine());
			double anglePrivilegie = myplace.getDirection12();
			/*if (triggered){
				anglePrivilegie = myplace.getDirection19();
			}
			else if (GC){
				anglePrivilegie = (Math.random()-0.5)*2*Math.PI;
			}
			else{
				anglePrivilegie = myplace.getDirection13();
			}*/
			
			double randomDirection = (Math.random()-0.5)*2*Math.PI;
			
			double vx = alphaCB*Math.cos(anglePrivilegie) + Math.cos(randomDirection);
			double vy = alphaCB*Math.sin(anglePrivilegie) + Math.sin(randomDirection);
			
			double newDirection = Math.atan2(vy, vx);
			
			RandomDataImpl distribution = new RandomDataImpl();
			double valeur = distribution.nextPoisson(vitesse_CB);
				
			positionspeed.changeSpeed(newDirection, valeur);
		}
	}

	public Cell transforme() {
		if (dt/taux_diff_CB_CC > Math.random()){
			type =Type.DeadCell;
			return new Centrocyt(this);
			
		}
		return new DeadCell();
	}

	@Override
	public void apoptosis() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dealWithChemokine() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void transition() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Cell Reproduction() {
		if ((dt/taux_division_CB > Math.random())){
			Centroblast cb = new Centroblast(this);
			antigen = antigen.mutated();
			return cb;
			
		}
		return new DeadCell();
	}

	public void interaction(Cell c) {
		/*if (c.type == Type.FollicularDendriticCell &(triggered == false)){
			triggered=true;
		}*/
		if((c.type == Type.TCell)&(triggered == true)){
			triggered = false;
			GC = true;
		}
		
	}

	

}
